<?php 
	header("content-type:text/html;charset=utf-8");
	$fileinfo = $_FILES['myFile'];
	$filename = $fileinfo['name'];
	$type = $fileinfo['type'];
	$tmp_name = $fileinfo['tmp_name'];
	$size = $fileinfo['size'];
	$error = $fileinfo['error'];

	$flag = true; //是否检测图片

	// 服务端做限制
	$maxSize = 2097152; //允许的最大值

	$allowExt = array('jpeg','jpg','png','gif','wbmp'); //允许上传的类型

	if ($error == UPLOAD_ERR_OK) {
		//判断上传文件大小
		if ($size > $maxSize) {
			exit('上传文件过大');
		}
		// 拆分字符串获得扩展名
		// $ext = strtolower(end(explode('.', $filename)));
		// 或者
		$ext = pathinfo($filename,PATHINFO_EXTENSION);

		if (!in_array($ext, $allowExt)) {
			exit('非法文件类型');
		}

		if (!is_uploaded_file($tmp_name)) {
			exit('文件不是通过HTTP POST方式上传的');
		}

		// 检测是否是一个真实的图片类型
		if ($flag) {
			if (!getimagesize($tmp_name)) {
				exit('不是真实的图片类型');
			}
		}

		$path = 'uploads';
		if (file_exists($path)) {
			mkdir($path,0777,true);
			chmod($path, 0777);
		}
		//确保文件名唯一，防止覆盖
		$uniName = md5(uniqid(microtime(true),true)) . '.' . $ext;
		$destination = $path . '/' . $uniName;
		if (move_uploaded_file($tmp_name, $destination)) {
			echo "上传成功";
		}else{
			echo "上传失败";
		}
	}else{
		switch ($error) {
			case 1:
				echo "大小超过限制";
				break;

			case 2:
				echo "大小超过了html表单中MAX_FILE_SIZE选项指定值";
				break;

			case 3:
				echo "文件只有部分被上传";
				break;

			case 4:
				echo "没有文件被上传";
				break;

			case 6:
				echo "找不到临时文件夹";
				break;

			case 7:
				echo "文件写入失败";
				break;

			case 8:
				echo "上传文件被PHP扩展文件中断";
				break;
			
			default:
				
				break;
		}
	}